<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/base.js"></script>
</head>
<body>
<h3 id="welcome"></h3>
<table border="1px" id="courseData">

</table>
<script>

    function init() {
        let username=localStorage.getItem("username");
        document.getElementById("welcome").innerHTML=username+"的已选课程";
        let url="http://localhost:9001/Demo/user/course";
        let method="get";
        $.rest(method,url,null,function (r) {
            if(r.ok) {
                //装配数据
                let innerHTML = "  <tr>\n" +
                    "        <td>课程号</td>\n" +
                    "        <td>课程名称</td>\n" +
                    "        <td>授课教师</td>\n" +
                    "        <td>教室容量</td>\n" +
                    "        <td>当前选课人数</td>\n" +
                    "        <td>操作</td>\n" +
                    "    </tr>";
                let courses = r.result.courses;
                for (let i = 0; i < courses.length; i++) {
                    let course = courses[i];
                    let courseHtml = "<tr>\n" +
                        "        <td>" + course.id + "</td>\n" +
                        "        <td>" + course.name + "</td>\n" +
                        "        <td>" + course.username + "</td>\n" +
                        "        <td>" + course.sMax + "</td>\n" +
                        "        <td>" + course.sSum + "</td>\n" +
                        "        <td><a href=\"#\"onclick=\"return onUnSelectedClient(" + course.id + ")\">不选这门课</a></td>\n" +
                        "    </tr>";
                    innerHTML += courseHtml;
                }
                document.getElementById("courseData").innerHTML = innerHTML;

            }
        });
    }
    window.onload=init();
    function onUnSelectedClient(cid) {
        let url="http://localhost:9001/Demo/user/course";
        let method="delete";
        let  json={}
        json.cid=cid;
        $.rest(method,url,json,function (r) {
        if(r.ok){
            alert("删除成功")
            init();
        }
        });


    }
</script>
</body>
</html>